from __future__ import absolute_import, division

from .base import *
from .events import *

from direct.showbase.ShowBase import ShowBase


class PandaPrimitive(GUIPrimitive):
    show_base = Attribute(initiable = False)
    window    = show_base._.win
    scene     = show_base._.aspect2d
    mouse     = show_base._.mouseWatcherNode
    
    clock     = Attribute(initiable = False)
    resources = Attribute(initiable = False)
    
    def set_config(self, name, *values):
        assert self.show_base is None
        
        loadPrcFileData('', ' '.join(str(item) for item in (name,) + values))
    
    def add_event(self, event):
        self.show_base.accept(event.id, event.handle)
    
    def configure(self):
        self.set_config('text-encoding', 'utf-8')
    
    def setup_primitive(self):
        self.configure()
        
        self.setup_show_base()
        self.setup_scene()
        self.setup_clock()
        self.setup_components()
        self.setup_events()
        self.setup_tasks()
    
    def setup_show_base(self):
        self.show_base = ShowBase()
        self.show_base.disableMouse()
        self.show_base.setBackgroundColor(0.7, 0.8, 0.9)
    
    def setup_scene(self):
        self.scene.setScale(1)
                
        sun = self.show_base.render.attachNewNode(DirectionalLight('light-sun'))
        sun.setHpr(-60)
        
        ambience = self.show_base.render.attachNewNode(AmbientLight('light-ambience'))
        ambience.node().setColor((0.2, 0.2, 0.2, 1))
        
        self.show_base.render.setLight(sun)
        self.show_base.render.setLight(ambience)
        
    def setup_clock(self):
        self.clock = ClockObject.getGlobalClock()
        self.clock.setMode(ClockObject.MLimited)
        self.clock.setFrameRate(30)
    
    def setup_components(self):
        self.screen    = PandaScreen()
        self.resources = ResourceLoader(self.data_path, self.show_base.loader)
    
    def setup_events(self):
        self.add_event(WindowEvent(self.screen))
        self.add_event(MouseClickEvent(self.model))
    
    def setup_tasks(self):
        self.show_base.taskMgr.add(self.update, 'update')
    
    def set_debug_mode(self):
        self.set_config('notify-level',               'info')
        self.set_config('default-directnotify-level', 'info')
        self.set_config('show-frame-rate-meter ',          0)
    
    def set_title(self, title):
        if self.show_base is None:
            self.set_config('window-title', title)
        else:
            properties = WindowProperties()
            properties.setTitle(title)
            
            self.window.requestProperties(properties)
    
    def set_frame_size(self, width, height):
        if self.show_base is None:
            self.set_config('win-size', width, height)
        else:
            self.window.requestProperties(WindowProperties.size(width, height))
    
    def update(self, task):
        if self.window.isClosed():
            self.show_base.taskMgr.stop()
        else:
            self.model.update()
            self.show_base.render.setShaderInput('time', task.time)
        
        return task.cont
        
    def run(self):
        self.show_base.run()


class PandaScreen(Screen):
    x_range =_ (-1, 1)
    y_range =_ (1, -1)
